package com.zboy.zboycodeutil.controller;

import com.zboy.zboycodeutil.database.Util;
import com.zboy.zboycodeutil.templet.Code;
import com.zboy.zboycodeutil.xml.ConfigXml;
import com.zboy.zboycodeutil.xml.DatabaseXml;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class CodeController {

    //private Map<String, Map<String, String>> dbMap = ConfigXml.readConfig();

    @RequestMapping("generatorcode")
    public String generatorCode(@RequestBody Map<String, String> map) throws Exception {
        Map<String, Map<String, String>> dbMap = ConfigXml.readConfig();
        Map<String, String> propertyMap = dbMap.get(map.get("dbType"));
        Util util = new Util();
        util.setDbName(map.get("dbName"));//设置数据库名称
        util.setDbType(map.get("dbType"));//获取指定数据库的属性
        //拼接数据库的url jdbc:mysql://47.108.3.23:3306/zboy_test?characterEncoding=UTF8
        String url = propertyMap.get("url").replace("[ip]", map.get("ip"));
        url = url.replace("[db]", map.get("dbName"));

        util.setDriverName(propertyMap.get("driverName"));
        util.setUrl(url);
        util.setIp(map.get("ip"));
        util.setUserName(map.get("userName"));
        util.setPassWord(map.get("passWord"));
        if (url.indexOf("&amp;") < 0 && url.indexOf("&") > 0) {
            url = url.replace("&", "&amp;");
        }
        propertyMap.put("url", url);
        //获取当前文件夹下的模板目录下的所有文件夹
        String databaseXmlPath = new File("").getAbsolutePath() + File.separatorChar + "db";
        DatabaseXml.writeDatabaseXml(util, propertyMap, databaseXmlPath);

        //路径map封装
        Map<String, String> pathMap = new HashMap<String, String>();
        //获取当前文件夹下的模板目录下的所有文件夹
        String basePath = new File("").getAbsolutePath() + File.separatorChar + "模板" + File.separatorChar
                + map.get("templetName");//设定为当前文件夹

        pathMap.put("templetPath", basePath);//模板路径
        pathMap.put("projectTempletPath", basePath + File.separatorChar + "工程模板");
        pathMap.put("tablleTempletPath", basePath + File.separatorChar + "表级模板");
        pathMap.put("columnTempletPath", basePath + File.separatorChar + "列级模板");
        pathMap.put("xmlPath", map.get("xmlPath"));
        pathMap.put("codePath", map.get("codePath"));

        //全局替换符
        Map<String, String> publicMap = new HashMap<String, String>();
        publicMap.put("project", map.get("project"));
        publicMap.put("package", map.get("package"));

        publicMap.put("projectComment", map.get("projectComment"));
        publicMap.put("author", map.get("author"));

        publicMap.put("db", map.get("db"));
        publicMap.put("dbuser", map.get("dbuser"));
        publicMap.put("dbpassword", map.get("dbpassword"));
        String s = map.get("package").replace(".", ",");
        String[] paths = s.split(",");
        for (int i = 0; i < paths.length; i++) {
            publicMap.put("path_" + String.valueOf(i + 1), paths[i]);
        }
        //将3级包名包名大写作为启动类的类名 goods->GoodsApplication.java
        String appname = publicMap.get("path_3");
        publicMap.put("appname",appname.substring(0,1).toUpperCase()+appname.substring(1));
        publicMap.put("path_all", s.replace(",", "/"));
        //写db.xml
        DatabaseXml.writeDatabaseXml(util,propertyMap, map.get("xmlPath"));
        Code.create(pathMap, publicMap);//生成代码
        return "正在生成代码...";
    }

    @RequestMapping("getTempletList")
    public List getTempletList()throws Exception {
        ArrayList<String> list = new ArrayList<>();
        //获取当前文件夹下的模板目录下的所有文件夹
        String basePath = new File("").getAbsolutePath() + File.separatorChar + "模板";
        File file = new File(basePath);
        File[] files = file.listFiles();
        for (File f : files) {
            String name = f.getName();
            list.add(name);
        }
        return list;
    }

    /**
     * 查询数据库中的所有数据库
     *
     * @param mapData
     * @return
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    @RequestMapping("testConnect")
    public List<String> testConnect(@RequestBody Map<String, String> mapData) throws Exception {
        Map<String, Map<String, String>> dbMap = ConfigXml.readConfig();
        Util util = new Util();
        Map<String, String> map = dbMap.get(mapData.get("dbType"));
        util.setDriverName(map.get("driverName"));
        String url = map.get("url").replace("[ip]", mapData.get("ip")).replace("[db]", "");
        util.setUrl(url);
        util.setUserName(mapData.get("userName"));
        util.setPassWord(mapData.get("passWord"));
        List<String> catalogs = util.getSchemas();
        return catalogs;
    }


}
